package com.nageoffer.subject.application.controller;

import com.nageoffer.subject.application.convert.SubjectCategoryDTOConverter;
import com.nageoffer.subject.application.dto.SubjectCategoryDTO;
import com.nageoffer.subject.common.entity.Result;
import com.nageoffer.subject.domain.entity.SubjectCategoryBO;
import com.nageoffer.subject.domain.service.SubjectCategoryDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
public class SubjectgoryCategoryController {
    private final SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 新增题目分类
     */
    @PostMapping("/subject/category/addCategory")
    public Result<Boolean> categoryAdd(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter
                    .INSTANCE.convertDTOToCategoryBO(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.success(true);
        } catch (Throwable ex) {
            return Result.fail("新增题目分类!");
        }
    }

    /**
     * 查询刷题大分类
     */
    @PostMapping("/subject/category/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                    .convertDTOToCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter
                    .INSTANCE.convertCategoryBOListToCategoryDTOList(subjectCategoryBOList);
            return Result.success(subjectCategoryDTOList);
        } catch (Throwable ex) {
            return Result.fail("查询刷题大类分类失败!");
        }
    }

    /**
     * 查询刷题大类下的分类
     */
    @PostMapping("/subject/category/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                    .convertDTOToCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE
                    .convertCategoryBOListToCategoryDTOList(subjectCategoryBOList);
            return Result.success(subjectCategoryDTOList);
        } catch (Throwable ex) {
            return Result.fail("查询刷题大类下分类接口失败!");
        }
    }

    /**
     * 更新刷题分类
     */
    @PostMapping("/subject/category/updateCategory")
    public Result<Boolean> updateCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                    .convertDTOToCategoryBO(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.updateCategory(subjectCategoryBO);
            return Result.success(result);
        } catch (Throwable ex) {
            return Result.fail("修改刷题分类失败!");
        }
    }

    /**
     * 删除刷题分类
     */
    @PostMapping("/subject/category/deleteCategory")
    public Result<Boolean> deleteCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                    .convertDTOToCategoryBO(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.deleteCategory(subjectCategoryBO);
            return Result.success(result);
        } catch (Throwable ex) {
            return Result.fail("删除刷题分类失败!");
        }
    }
}
